package com.springboot.code.login.controller;

import com.spring.code.user.LoginUser;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * DESC:
 *
 * @author WangJie
 * @date Created in 2023-05-14 18:06
 */
@RestController
//@RequestMapping("/Redis")
public class RedisController {

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    //添加数据到Redis
    @PostMapping ("/addRedis")
    public String addRedis(String name, Integer age){
        //操作String中的string类型的数据创建ValueOperations对象
        ValueOperations valueOperations = redisTemplate.opsForValue();
        //添加数据到Redis
        valueOperations.set("lisi",23);
        return "向Redis添加String类型的数据";
    }

    //获取Redis中的数据
    @GetMapping ("/getRedis")
    public String getRedis(String k){
        //操作String中的string类型的数据创建ValueOperations对象
        ValueOperations valueOperations = redisTemplate.opsForValue();
        Object v = valueOperations.get(k);
        return "k="+k+"v="+v;
    }

    //使用路径变量添加数据和stringRedisTemplate
    @PostMapping("/setRedis/{k}/{v}")
    public String setRedis(@PathVariable String k, @PathVariable String v){
        stringRedisTemplate.opsForValue().set(k, v);
        return "使用stringRedisTemplate添加数据";
    }

    //获取对象
    @GetMapping("/ourRedis")
    public String ourRedis(String k){
        String s = stringRedisTemplate.opsForValue().get(k);
        return k+"的值是："+s;
    }

    /**
     * 设置RedisTemplate的序列化
     * 可以单独对k，v分别进行设置
     * 也可以同时设置
     * */
    @PostMapping("/addString")
    public String addString(String k, String v){
        //修改RedisTemplate的键(k)的序列化为字符串
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        //修改RedisTemplate的值(v)的序列化为字符串
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        redisTemplate.opsForValue().set(k,v);
        return "修改RedisTemplate的序列化";
    }

    /**
     * 使用json序列化 把java对象转化为json存储
     * */
    @PostMapping("/setJson")
    public String setJson(){
        LoginUser loginUser = new LoginUser("WangJie","12345");
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(LoginUser.class));
        redisTemplate.opsForValue().set("myJson",loginUser);
        return "使用JSON的序列化";
    }

    @GetMapping("/getJson")
    public String getJson(){
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(LoginUser.class));
        Object o = redisTemplate.opsForValue().get("myJson");
        return "JSON的反序列化:"+o;
    }
}
